package com.biubiu.biubiuexam.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.biubiu.biubiuexam.common.dto.CreateChoiceQuestionDto;
import com.biubiu.biubiuexam.common.dto.CreateSaQuestionDto;
import com.biubiu.biubiuexam.common.lang.Result;
import com.biubiu.biubiuexam.services.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 问题相关 controller
 */
@RestController
public class QuestionController {
    private final QuestionService questionService;

    /**
     * 自动装配构造函数
     *
     * @param questionService QuestionService 的实例
     */
    @Autowired
    public QuestionController(QuestionService questionService) {
        this.questionService = questionService;
    }

    /**
     * 创建选择题的接口
     *
     * @param createChoiceQuestionDto 前端创建选择题时传递的 Dto
     * @return 结果
     */
    @PostMapping("/teacher/question/createChoiceQuestion")
    public Result createChoiceQuestion(@Valid @RequestBody CreateChoiceQuestionDto createChoiceQuestionDto) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录！", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }
        if (createChoiceQuestionDto.getOptions().size() == 0) {
            return Result.fail(400, "选项不能没有！", null);
        }
        if (createChoiceQuestionDto.getAnswer() < 0 ||
                createChoiceQuestionDto.getAnswer() >= createChoiceQuestionDto.getOptions().size()) {
            return Result.fail(400, "答案不在合理范围内！", null);
        }
        return questionService.createChoiceQuestion(StpUtil.getLoginIdAsLong(), createChoiceQuestionDto);
    }


    /**
     * 创建简答题的接口
     *
     * @param createSaQuestionDto 前端创建简答题时传递的 Dto Sa = Short answer 简答题
     * @return 结果
     */
    @PostMapping("/teacher/question/createSaQuestion")
    public Result CreateSaQuestion(@Valid @RequestBody CreateSaQuestionDto createSaQuestionDto) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录！", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }
        return questionService.createSaQuestion(StpUtil.getLoginIdAsLong(), createSaQuestionDto);
    }

    /**
     * 删除问题的接口
     *
     * @param link 问题的链接
     * @return 结果
     */
    @PostMapping("/teacher/question/delQuestion")
    public Result DeleteQuestion(@RequestBody String link) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录！", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }
        return questionService.deleteQuestion(StpUtil.getLoginIdAsLong(), link);
    }

    /**
     * 获取自己出的所有题目接口
     *
     * @return 自己出的所有题目
     */
    @GetMapping("/teacher/question/getMyAllQuestions")
    public Result getMyAllQuestions() {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录！", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return questionService.getMyAllQuestions(StpUtil.getLoginIdAsLong());
    }

}
